/*
 * Copyright (C) 2020-present, kinbug All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.kinbug.sniper.common.entity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import com.gitee.kinbug.sniper.common.enums.OrderActionEnum;

import lombok.Data;

/**
 * -描述
 * @ClassName: Statistics
 * @author kinbug
 * @date 2020-12-30
 */
@Data
public class Statistics {

	private BigDecimal longGainNum = BigDecimal.ZERO;
	private BigDecimal longGainAmount = BigDecimal.ZERO;

	private BigDecimal longLossNum = BigDecimal.ZERO;
	private BigDecimal longLossAmount = BigDecimal.ZERO;

	private BigDecimal shortGainNum = BigDecimal.ZERO;
	private BigDecimal shortGainAmount = BigDecimal.ZERO;

	private BigDecimal shortLossNum = BigDecimal.ZERO;
	private BigDecimal shortLossAmount = BigDecimal.ZERO;

	/**
	 * -添加一次做多盈利
	 * @Title addLongGain
	 * @param  参数
	 * @return void 返回类型
	 * @throws
	 */
	public void addLongGain(BigDecimal longGainAmount) {
		this.setLongGainNum(longGainNum.add(BigDecimal.ONE));
		this.setLongGainAmount(this.longGainAmount.add(longGainAmount));
	}

	/**
	 * -添加一次做多亏损
	 * @Title addLongGain
	 * @param  参数
	 * @return void 返回类型
	 * @throws
	 */
	public void addLongLoss(BigDecimal longLossAmount) {
		this.setLongLossNum(longLossNum.add(BigDecimal.ONE));
		this.setLongLossAmount(this.longLossAmount.add(longLossAmount));
	}

	/**
	 * -添加一次做空盈利
	 * @Title addLongGain
	 * @param  参数
	 * @return void 返回类型
	 * @throws
	 */
	public void addShortGain(BigDecimal shortGainAmount) {
		this.setShortGainNum(shortGainNum.add(BigDecimal.ONE));
		this.setShortGainAmount(this.shortGainAmount.add(shortGainAmount));
	}

	/**
	 * -添加一次做空亏损
	 * @Title addLongGain
	 * @param  参数
	 * @return void 返回类型
	 * @throws
	 */
	public void addShortLoss(BigDecimal shortLossAmount) {
		this.setShortLossNum(shortLossNum.add(BigDecimal.ONE));
		this.setShortLossAmount(this.shortLossAmount.add(shortLossAmount));
	}

	
	/**
	 * -做多次数
	 * @Title getLongWinRate
	 * @param  参数
	 * @return BigDecimal 返回类型
	 * @throws
	 */
	public BigDecimal getLongNum() {
		return longGainNum.add(longLossNum);
	}
	
	/**
	 * -做多总盈利
	 * @Title getLongWinRate
	 * @param  参数
	 * @return BigDecimal 返回类型
	 * @throws
	 */
	public BigDecimal getLongGains() {
		return this.longGainAmount.add(this.longLossAmount);
	}
	
	/**
	 * -做多胜率
	 * @Title getLongWinRate
	 * @param  参数
	 * @return BigDecimal 返回类型
	 * @throws
	 */
	public BigDecimal getLongWinRate() {
		if (longGainNum.intValue()>0) {
			return longGainNum.divide(this.getLongNum(),4, RoundingMode.DOWN).multiply(new BigDecimal(100)).setScale(2, RoundingMode.DOWN);
		}else {
			return BigDecimal.ZERO;
		}
		
	}
	
	/**
	 * -做空次数
	 * @Title getLongWinRate
	 * @param  参数
	 * @return BigDecimal 返回类型
	 * @throws
	 */
	public BigDecimal getShortNum() {
		return shortGainNum.add(shortLossNum);
	}
	/**
	 * -做空总盈利
	 * @Title getLongWinRate
	 * @param  参数
	 * @return BigDecimal 返回类型
	 * @throws
	 */
	public BigDecimal getShortGains() {
		return this.shortGainAmount.add(this.shortLossAmount);
	}
	
	/**
	 * -做空胜率
	 * @Title getLongWinRate
	 * @param  参数
	 * @return BigDecimal 返回类型
	 * @throws
	 */
	public BigDecimal getShortWinRate() {
		if (shortGainNum.intValue()>0) {
			return shortGainNum.divide(this.getShortNum(),4, RoundingMode.DOWN).multiply(new BigDecimal(100)).setScale(2, RoundingMode.DOWN);
		}else {
			return BigDecimal.ZERO;
		}
		
	}
	
	/**
	 * -总盈利
	 * @Title getLongWinRate
	 * @param  参数
	 * @return BigDecimal 返回类型
	 * @throws
	 */
	public BigDecimal getTotalGains() {
		return this.getLongGains().add(this.getShortGains());
	}
	
	/**
	 * -总胜率
	 * @Title getLongWinRate
	 * @param  参数
	 * @return BigDecimal 返回类型
	 * @throws
	 */
	public BigDecimal getTotalWinRate() {
		if (longGainNum.intValue()>0||shortGainNum.intValue()>0) {
			return (longGainNum.add(shortGainNum)).divide(this.getLongNum().add(this.getShortNum()),4, RoundingMode.DOWN).multiply(new BigDecimal(100)).setScale(2, RoundingMode.DOWN);
		}else {
			return BigDecimal.ZERO;
		}
		
	}
	
	/**
	 * -描述
	 * @Title addEarnings
	 * @param  参数
	 * @return void 返回类型
	 * @throws
	 */
	public void addEarnings(Earning earning) {
		if (earning.getOrderAction() == OrderActionEnum.BUYUP) {
			if (earning.getEarn().compareTo(BigDecimal.ZERO) > 0) {
				this.addLongGain(earning.getEarn());
			}else {
				this.addLongLoss(earning.getEarn());
			}
		}else {
			if (earning.getEarn().compareTo(BigDecimal.ZERO) > 0) {
				this.addShortGain(earning.getEarn());
			}else {
				this.addShortLoss(earning.getEarn());
			}
		}
	}
}
